$(function() {




	var token = localStorage.phone;
	var address = localStorage.address;
	var order_time = localStorage.order_time;
	var subject = localStorage.subject;
	var teacher_id = localStorage.teacher_id;
	var today = $.grep(localStorage.today.split(","),  function(n)  {
		return  $.trim(n).length  >  0;
	});
	var tomorrow = $.grep(localStorage.tomorrow.split(","),  function(n)  {
		return  $.trim(n).length  >  0;
	});
	var after = $.grep(localStorage.after.split(","),  function(n)  {
		return  $.trim(n).length  >  0;
	});
	var place = localStorage.place;
	//计算出数量
	var num = today.length + tomorrow.length + after.length;
	//计算出价格
	var placeAll = num * place;
	$('#place').html(placeAll + '元');
	var ALIPAYSERVER = '';

	function goback() {
		history.back();
	}
	var wxChannel = null; // 微信支付
	var aliChannel = null; // 支付宝支付
	var channel = null;
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});

	mui.plusReady(function() {
		// 获取支付通道
		plus.payment.getChannels(function(channels) {
			aliChannel = channels[0];
			wxChannel = channels[1];
		}, function(e) {
			alert("获取支付通道失败：" + e.message);
		});
	})

	document.getElementById('butt').addEventListener('tap', function() {
		//保存訂單
		var payment_type = $("input[name='payment']").val();
		$("input[name='payment_type']").each(function() {
			if($(this).prop('checked')) {
				payment_type = $(this).val();
			}
		});
		if(payment_type == '') {
			layer.msg("请选择支付方式");
		} else {
			$.ajax({
				url: "https://newapp.icandrive.cn/api/saveTeacherOrder.php",
				type: 'post',
				data: {
					token: token,
					teacher_id: teacher_id,
					today: today,
					tomorrow: tomorrow,
					after: after,
					subject: subject,
					address: address,
					order_time: order_time,
					place: place,
					gangshao:localStorage.yue_messages
				},
				dataType: 'json',
				type: 'post',
				success: function(data) {
					if(data.status == 200) {
						var order = data.order;
						localStorage.teacher_order = order ;
						ALIPAYSERVER = 'https://newapp.icandrive.cn/api/teacher_pay.php?place=' + placeAll + "&order_number=" + order;
						pay('alipay');
						localStorage.type = "分享订单";
						setInterval(function(){
							window.location.herf="../alaert_info/alert_wsc.html";
						},3000)
						  } else {
						layer.msg("保存订单失败");
					}
				},
				error: function() {
					layer.msg('系统繁忙', function() {
						history.go(0)
					}, 1000);
				}
			});
			$(this).unbind();
		}

	})

	function pay(id) {
		/*console.log(ALIPAYSERVER);*/
		PAYSERVER = ALIPAYSERVER;
		channel = aliChannel;
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function() {
			switch(xhr.readyState) {
				case 4:
					if(xhr.status == 200) {
						plus.payment.request(channel, xhr.responseText, function(result) {
							plus.nativeUI.alert("支付成功！", function() {
								/*请求发送培训订单生产短信通知接口*/
								$.ajax({
									url: "https://newapp.icandrive.cn/api/yuejiao.php",
									type: 'post',
									dataType: 'json',
									data: {
										order_number: localStorage.teacher_order
									},
									success: function() {

									}
								});
								 window.location.href = "../index/index.html";

							});
						}, function(error) {
							plus.nativeUI.alert("支付失败：" + error.code);
						});
					} else {
						alert("获取订单信息失败！");
					}
					break;
				default:
					break;
			}
		}
		xhr.open('GET', PAYSERVER);
		xhr.send();
	}
})

//设置根元素字体
setFontSize(1080);
